package com.example.zhujiemian;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zhujiemian.Bean.Student;
import com.example.zhujiemian.db.DBHelper;
@SuppressLint("MissingInflatedId")
public class AddStudent extends AppCompatActivity  {
    private TextView mtextView;
    private EditText mEditName;
    private EditText mEditCode;
    private EditText mEditClass;
    private EditText mEditUsername;
    private EditText mEditPassword;
    private Button mAddStudentOk;
    private DBHelper DBHelper;
    private Button mStdudentBackSet;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_student);
        DBHelper =new DBHelper(this);
        mAddStudentOk=findViewById(R.id.AddStudentOk);
        mStdudentBackSet=findViewById(R.id.StdudentBackSet);

        mStdudentBackSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mStdudentBackSet.setVisibility(View.VISIBLE);
                Intent intent=new Intent(AddStudent.this,Setting.class);
                startActivity(intent);
            }
        });
        mAddStudentOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=mEditName.getText().toString();
                String code = mEditCode.getText().toString();
                String username = mEditUsername.getText().toString();
                String password = mEditPassword.getText().toString();
                String className = mEditClass.getText().toString();
                if (v.getId() == R.id.buttonAddStudent){
                    if (allInputNotEmpty(name, code, username, password, className)) {
                        Student student = new Student();
                        student.setStudent_code(code);
                        student.setStudent_name(name);
                        student.setUsername(username);
                        student.setPassword(password);
                        student.setClass_name(className);
                        int id= DBHelper.insertStudent2(student);
                        if (id != -1) {
                            Toast.makeText(AddStudent.this, "添加成功", Toast.LENGTH_SHORT).show();
                            mEditName.setText("");
                            mEditCode.setText("");
                            mEditUsername.setText("");
                            mEditPassword.setText("");
                            mEditClass.setText("");
                        }else {
                            Toast.makeText(AddStudent.this, "添加失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
            private boolean allInputNotEmpty(String... inputStrings) {
                for (int i = 0; i < inputStrings.length; i++) {
                    if (TextUtils.isEmpty(inputStrings[i])) {
                        return false;
                    }
                }
                return true;
            }
        });

        }
    }